package dtaskboard.client.toolbar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.DialogUtil;
import dtaskboard.client.dto.RoleClass;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;
import dtaskboard.client.util.ListUtil;

public class AddRoleToUserDialog extends DialogBox {

  private final TextBox user;
  private final ListBox role;
  private final TextBox project;

  public AddRoleToUserDialog() {
    this.user = new TextBox();
    this.project = new TextBox();
    this.role = new ListBox(false);

    ListUtil.populate(this.role, RoleClass.class);

    Panel panel = new VerticalPanel();

    Label projectLabel = new Label("Nome do projeto");
    Label userLabel = new Label("Nome do usuario");
    Label roleLabel = new Label("Permissão");

    panel.add(userLabel);
    panel.add(this.user);
    panel.add(roleLabel);
    panel.add(this.role);
    panel.add(projectLabel);
    panel.add(this.project);
    panel.add(new AddRoleToUserSubmitButton());

    super.setAnimationEnabled(true);
    super.setAutoHideEnabled(true);
    super.setHTML("Adicionando papel ao usuario");
    super.setGlassEnabled(true);
    super.setModal(true);
    super.setWidget(panel);
    super.setVisible(true);
  }

  @Override
  public void show() {
    this.user.setText("");
    this.project.setText("");
    this.role.setSelectedIndex(-1);
    super.show();
  }


  class AddRoleToUserSubmitButton extends Button implements ClickHandler {
    public AddRoleToUserSubmitButton() {
      this.setText("Adicionando papel ao usuario");
      this.addClickHandler(this);
    }

    @Override
    public void onClick(ClickEvent event) {
      LoginServiceAsync loginService = GWT.create(LoginService.class);
      final AddRoleToUserDialog dialog = AddRoleToUserDialog.this;
      String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
      String role = ListUtil.getValue(dialog.role);
      RoleClass roleClass = RoleClass.valueOf(role);
      loginService.addRoleToUser(dialog.user.getText(), roleClass,
          dialog.project.getText(), sessionId, new AsyncCallback<Void>() {

            @Override
            public void onFailure(Throwable caught) {
              DialogUtil.closableDialog("Erro",
                  "Nao foi possivel dar a role ao usuario.");
            }

            @Override
            public void onSuccess(Void result) {
              DialogUtil.closableDialog("Role de usuario",
                  "Role adicionada com sucesso");
              dialog.hide();
            }

          });
    }
  }
}
